package com.cino.util;

/**
 * ThreadLocal工具类，用于存储和获取ID
 */
public class ThreadLocalUtil {
    
    private ThreadLocalUtil() {
        // 私有构造函数，防止实例化
    }
    
    private static final ThreadLocal<String> ID_THREAD_LOCAL = new ThreadLocal<>();
    private static final ThreadLocal<Boolean> CAPTURE_ID_FLAG = new ThreadLocal<>();
    private static final ThreadLocal<String> CHANGE_CONTENT_THREAD_LOCAL = new ThreadLocal<>();
    private static final ThreadLocal<String[]> INCLUDE_FIELDS_THREAD_LOCAL = new ThreadLocal<>();
    private static final ThreadLocal<String> OBJECT_TYPE_THREAD_LOCAL = new ThreadLocal<>();
    
    /**
     * 设置ID
     */
    public static void setId(String id) {
        ID_THREAD_LOCAL.set(id);
    }
    
    /**
     * 获取ID
     */
    public static String getId() {
        return ID_THREAD_LOCAL.get();
    }
    
    /**
     * 设置是否需要捕获ID的标记
     */
    public static void setCaptureId(boolean capture) {
        CAPTURE_ID_FLAG.set(capture);
    }
    
    /**
     * 判断是否需要捕获ID
     */
    public static boolean shouldCaptureId() {
        return Boolean.TRUE.equals(CAPTURE_ID_FLAG.get());
    }
    
    /**
     * 设置变更内容
     */
    public static void setChangeContent(String content) {
        CHANGE_CONTENT_THREAD_LOCAL.set(content);
    }
    
    /**
     * 获取变更内容
     */
    public static String getChangeContent() {
        return CHANGE_CONTENT_THREAD_LOCAL.get();
    }
    
    /**
     * 设置需要审计的字段
     */
    public static void setIncludeFields(String[] fields) {
        INCLUDE_FIELDS_THREAD_LOCAL.set(fields);
    }
    
    /**
     * 获取需要审计的字段
     */
    public static String[] getIncludeFields() {
        return INCLUDE_FIELDS_THREAD_LOCAL.get();
    }
    
    /**
     * 设置对象类型（表名）
     */
    public static void setObjectType(String objectType) {
        OBJECT_TYPE_THREAD_LOCAL.set(objectType);
    }
    
    /**
     * 获取对象类型（表名）
     */
    public static String getObjectType() {
        return OBJECT_TYPE_THREAD_LOCAL.get();
    }
    
    /**
     * 清理ThreadLocal
     */
    public static void clear() {
        ID_THREAD_LOCAL.remove();
        CAPTURE_ID_FLAG.remove();
        CHANGE_CONTENT_THREAD_LOCAL.remove();
        INCLUDE_FIELDS_THREAD_LOCAL.remove();
        OBJECT_TYPE_THREAD_LOCAL.remove();
    }
}